#include <iostream>

using namespace std;

struct TreeLinkNode {
	int val;
	struct TreeLinkNode *left;
	struct TreeLinkNode *right;
	struct TreeLinkNode *next;
	TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {

	}
};


TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
	if (pNode == NULL) return NULL;
	TreeLinkNode *p;
	if (pNode->right != NULL)
	{
		p = pNode->right;
		while (p->left != NULL)
			p = p->left;
		return p;
	}
	/*else if (pNode = pNode->next->left)
		return pNode->next;*/
	else
	{
		p = pNode;
		while (p != p->next->left && p->next != NULL)
			p = p->next;
		return p->next;
	}

}
